/* Created by FINAL PYTON 88 */

package pl.edu.agh.admin.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import pl.edu.agh.admin.api.CronTabManager;
import pl.edu.agh.admin.api.CronTabManagerProvider;
import pl.edu.agh.admin.api.Task;
import pl.edu.agh.admin.api.User;




public class TimeAxisPanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 14;

	private JRadioButton dayRadioButton;
	private JRadioButton hourRadioButton;
	private JRadioButton minuteRadioButton;
	private Scale scale;
	List<User> selectedUsers = new LinkedList<User>();

	static final long weekInMs   = 604800000L;
	static final long dayInMs    = 86400000L;
	static final long hourInMs   = 3600000L;
	static final long minuteInMs = 60000L;
	
	
	public TimeAxisPanel()
	{	
		JLabel label = new JLabel("Os czasu zadan uzytkownikow ");
		add(label);

		scale = Scale.DAY;
		dayRadioButton = new JRadioButton("dni");
		dayRadioButton.setSelected(true);
		dayRadioButton.addActionListener(this);
		add(dayRadioButton);

		hourRadioButton = new JRadioButton("godziny");
		hourRadioButton.addActionListener(this);
		add(hourRadioButton);
		
		minuteRadioButton = new JRadioButton("minuty");
		minuteRadioButton.addActionListener(this);
		add(minuteRadioButton);

		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(dayRadioButton);
		buttonGroup.add(hourRadioButton);
		buttonGroup.add(minuteRadioButton);
		
		
	}



	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == dayRadioButton)
			scale = Scale.DAY;
		else if(e.getSource() == hourRadioButton)
			scale = Scale.HOUR;
		else if(e.getSource() == minuteRadioButton)
			scale = Scale.MINUTE;

		repaint();
	}
	
	
	
	public void paint(Graphics g)
	{
		super.paint(g);
		Graphics2D g2 = (Graphics2D)g;
		
		
		CronTabManager cronTabManager = CronTabManagerProvider.getCronTabManager();
		List<Task> tasks = new LinkedList<Task>();
		for(User u : selectedUsers)
			tasks.addAll(cronTabManager.getUserTasks(u));
		
		
		/*
		SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		for(Task t : tasks)
		{
			for(Calendar c : t.getAllMomentsOfExecution(end))
			{
				System.out.println(dateformatter.format(c.getTime()));
			}
		}
		*/
		
		
		long scaleMs = 0L;
		switch(scale)
		{
			case DAY:
				scaleMs = weekInMs;  //604800000L; //1w
				break;
			case HOUR:
				scaleMs = dayInMs; //86400000L; //1d
				break;
			case MINUTE:
				scaleMs = hourInMs; //3600000L; //1h
				break;
		}
		
		
		final int panelWidth = getWidth();
		final int barWidth = panelWidth - 20;
		final int barHeight = 50;

		final long beginMs = Calendar.getInstance().getTimeInMillis();
		final long endMs = beginMs + scaleMs;

		

		g2.draw(new Rectangle2D.Double(10, 30, barWidth, barHeight));

		Calendar begin = Calendar.getInstance();
		long scaleUnitMs = 0;
		int scaleUnits = 0;
		switch(scale)
		{
			case DAY:
				FinalPytonCalendar88Utils.toNearestWholeDay(begin);
				scaleUnitMs = dayInMs;
				scaleUnits = 7;
				break;
			case HOUR:
				FinalPytonCalendar88Utils.toNearestWholeHour(begin);
				scaleUnitMs = hourInMs;
				scaleUnits = 24;
				break;
			case MINUTE:
				FinalPytonCalendar88Utils.toNearestWholeMinute(begin);
				scaleUnitMs = minuteInMs;
				scaleUnits = 60;
				break;
		}
		Calendar end = (Calendar)begin.clone();
		end.add(Calendar.HOUR_OF_DAY, 24);
		
		for(int i = 1; i < scaleUnits; i++)
		{
			double fp = (double)((beginMs + i * scaleUnitMs) - beginMs) / (double)scaleMs;
			g2.draw(new Line2D.Double(10 + fp * barWidth, 25 + barHeight, 10 + fp * barWidth, 35 + barHeight));
		}
		
		


		Calendar testCalendar = Calendar.getInstance();
		testCalendar.add(Calendar.HOUR_OF_DAY, 18);

		
		//Tasks
		for(Task t : tasks)
		{
			for(Calendar c : t.getAllMomentsOfExecution(end))
			{
			    double tmp = (double)(c.getTimeInMillis() - beginMs) / (double)scaleMs;
			    g2.setColor(FinalPytonUserColorier.getUserColor(t.getUser()));
				g2.draw(new Line2D.Double(10 + tmp * barWidth, 30, 10 + tmp * barWidth, 30 + barHeight));
			}
		}
	}

	
	
	public void setSelectedUsers(List<User> users)
	{
		selectedUsers = users;
	}
	

	

}



enum Scale
{
	DAY, HOUR, MINUTE
};
